<template>
  <div class="container">
    <div class="header">我的订单</div>
    <van-tabs v-model:active="active" @click-tab="onClickTab">
      <van-tab title="全部"    name="0"></van-tab>
      <van-tab title="待支付"  name="1"></van-tab>
      <van-tab title="待服务"  name="2"></van-tab>
      <van-tab title="已完成"  name="3"></van-tab>
      <van-tab title="已取消"  name="4"></van-tab>
    </van-tabs>
    <!-- 列表 -->
    <van-row class="yy-list" justify="space-around" v-for="(item,index) in order"
             :key="item.code"
             @click="goDetail(item)">
      <van-col span="3">
        <!-- image -->
        <van-image width="50" height="50" radius="10" src="http://localhost:8090/image/push.png" />
      </van-col>
      <van-col class="yy" span="15" >
        <div class="text1">就医尊享</div>
        <div class="text2">
          <div> {{item.hospitalName}} </div>
          <div> 预约时间：{{item.startTime}} </div>
        </div>
      </van-col>
      <van-col class="text2" :style="{ color: colorMap[ statusMap[item.status] ]}" span="3" >
        {{ statusMap[item.status] }}
        <counter :second="item.timer" v-if="item.status==1 && item.timer > 0" />
      </van-col>
    </van-row>

  </div>
</template>

<script setup>
import {ref, reactive, onMounted, getCurrentInstance} from 'vue'
import counter from '../../components/counter.vue'
import { useRouter, useRoute} from 'vue-router'
const router = useRouter();
const route= useRoute();

const order = ref([])
// 获取当前vue实例
const {proxy} = getCurrentInstance();

const active = ref();
const statusMap = {
  1 : '待支付',
  2 : '待服务',
  3 : '已完成',
  4 : '已取消',
  10 : '订单已超时'
}
const colorMap = {
  '待支付': '#ffa200',
  '待服务': '#1da6fd',
  '已完成': '#21c521',
  '订单已超时': '#FF0000',
}

// 获取订单列表
const getOrderList = async (status) => {

  const { data } = await proxy.$api.orderList({status: status});
  //console.log('orderList ',data);
  // 倒计时
  data.data.forEach(item => {
    const dateTimeStr = item.createTime.trim();
    if (dateTimeStr) {
      const timestamp = new Date(dateTimeStr).getTime();
      item.timer = timestamp + 7200000 - Date.now();
      // 订单超时
      if (item.status == 1 && item.timer < 0){
        item.status = 10;
      }

    }
  });


  order.value = data.data;
}
// 跳转详情, 附带参数
const goDetail = (item) => {

  router.push({
    path: '/detail',
    query: {
      orderId: item.id
    }
  });
}
const onClickTab = (item) => {
  getOrderList(item.name);
}
onMounted(async () => {

  await getOrderList(0);

});

</script>

<style lang="less" scoped>
.container {
  background-color: #f0f0f0;
  height: 100vh;
}
.header {
  background-color: #fff;
  line-height: 40px;
  text-align: center;
}
.van-row {
  background-color: #fff;
  padding: 10px;
  margin: 5px;
  border-radius: 5px;
  .text1 {
    font-size: 16px;
    line-height: 25px;
    font-weight: bold;
  }
  .text2 {
    font-size: 14px;
    line-height: 20px;
    color: #999999;
  }
}
.bottom-text {
  line-height: 50px;
  text-align: center;
  color: #999999;
}
</style>
